package top.huhuiyu.study.dao;

import top.huhuiyu.study.entity.TbCpuInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class TbCpuInfoDAO {
    private static final String INSERT = "insert into tb_cpu_info(cpu,info,cores,threads,frequency,max_frequency,post_time) values(?,?,?,?,?,?,?)";
    private static final String QUERY = "select * from tb_cpu_info";
    private static final String QUERY_BY_ID = "select * from tb_cpu_info where id = ?";
    public static final String UPDATE = "update tb_cpu_info set cpu = ?,info = ?,cores = ?,threads = ?,frequency = ?,max_frequency = ?,post_time = ? where id = ?";
    public static final String DELETE = "delete from tb_cpu_info where id = ?";

    public static int delete(Integer id) throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(DELETE);
        ps.setInt(1, id);
        int i = ps.executeUpdate();
        conn.close();
        return i;
    }

    public static int update(TbCpuInfo info) throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(UPDATE);
        ps.setString(1, info.getCpu());
        ps.setString(2, info.getInfo());
        ps.setInt(3, info.getCores());
        ps.setInt(4, info.getThreads());
        ps.setString(5, info.getFrequency());
        ps.setString(6, info.getMax_frequency());
        ps.setTimestamp(7, new Timestamp(info.getPost_time().getTime()));
        ps.setInt(8, info.getId());
        int i = ps.executeUpdate();
        conn.close();
        return i;
    }

    public static int insert(TbCpuInfo info) throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(INSERT);
        ps.setString(1, info.getCpu());
        ps.setString(2, info.getInfo());
        ps.setInt(3, info.getCores());
        ps.setInt(4, info.getThreads());
        ps.setString(5, info.getFrequency());
        ps.setString(6, info.getMax_frequency());
        ps.setTimestamp(7, new Timestamp(info.getPost_time().getTime()));
        int i = ps.executeUpdate();
        conn.close();
        return i;
    }

    public static List<TbCpuInfo> query() throws Exception {
        Connection conn = DBHelper.getConnection();
        List<TbCpuInfo> list = new ArrayList<>();
        PreparedStatement ps = conn.prepareStatement(QUERY);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            TbCpuInfo info = new TbCpuInfo();
            info.setId(rs.getInt("id"));
            info.setCpu(rs.getString("cpu"));
            info.setInfo(rs.getString("info"));
            info.setCores(rs.getInt("cores"));
            info.setThreads(rs.getInt("threads"));
            info.setFrequency(rs.getString("frequency"));
            info.setMax_frequency(rs.getString("max_frequency"));
            info.setPost_time(rs.getTimestamp("post_time"));
            list.add(info);
        }
        conn.close();
        return list;
    }

    public static TbCpuInfo queryById(Integer id) throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(QUERY_BY_ID);
        ps.setInt(1, id);
        ResultSet rs = ps.executeQuery();
        TbCpuInfo info = null;
        if (rs.next()) {
            info = new TbCpuInfo();
            info.setId(rs.getInt("id"));
            info.setCpu(rs.getString("cpu"));
            info.setInfo(rs.getString("info"));
            info.setCores(rs.getInt("cores"));
            info.setThreads(rs.getInt("threads"));
            info.setFrequency(rs.getString("frequency"));
            info.setMax_frequency(rs.getString("max_frequency"));
            info.setPost_time(rs.getTimestamp("post_time"));
        }
        conn.close();
        return info;
    }


}
